package com.dgshanger.sanhxiaofeisc.ui.home.activity

import android.content.Intent
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.dgshanger.sanhxiaofeisc.R
import com.dgshanger.sanhxiaofeisc.adapter.ShoppingVideoAdapter
import com.dgshanger.sanhxiaofeisc.entity.ShoppingVideoEntity
import com.dgshanger.sanhxiaofeisc.mvp.contract.ShoppingVideoContract
import com.dgshanger.sanhxiaofeisc.mvp.presenter.ShoppingVideoPresenter
import com.dgshanger.sanhxiaofeisc.ui.WebActivity
import com.xx.baseuilibrary.mvp.lcec.BaseMvpLcecActivity
import kotlinx.android.synthetic.main.activity_shopping_video.*

/**
 * Created by JacobHHH on 2018/4/22.
 * 3h视频
 */
class ShoppingVideoActivity : BaseMvpLcecActivity<SwipeRefreshLayout, List<ShoppingVideoEntity>, ShoppingVideoContract.Model, ShoppingVideoContract.View, ShoppingVideoPresenter>(), ShoppingVideoContract.View {

    private lateinit var mAdapter: ShoppingVideoAdapter

    override fun createPresenter(): ShoppingVideoPresenter = ShoppingVideoPresenter()

    override fun getActivityLayoutId(): Int = R.layout.activity_shopping_video

    private var currentPage: Int = 1

    override fun initData() {
        super.initData()
        initView()
        loadData(true)
    }

    private fun initView() {
        recyclerView.layoutManager = LinearLayoutManager(mContext)
        mAdapter = ShoppingVideoAdapter(R.layout.item_shopping_video, ArrayList())
        mAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN)
        recyclerView.adapter = mAdapter
        mAdapter.setOnLoadMoreListener {
            loadData(false)
        }

    }

    override fun initEvent() {
        //刷新
        contentView.setOnRefreshListener {
            if (!presenter.isLoading!!) {
                currentPage = 1
                loadData(true)
            }
        }

        recyclerView.addOnItemTouchListener(object : OnItemClickListener() {
            override fun onSimpleItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
                val shoppingVideoEntity = mAdapter.data[position]
                if ("1".equals(shoppingVideoEntity.video_type)) {
                    //内链,视频Video播放
                    val intent = Intent(mContext, HomeVideo2Activity::class.java)
                    intent.putExtra("title", shoppingVideoEntity.video_title)
                    intent.putExtra("videoUrl", shoppingVideoEntity.video_url)
                    startActivity(intent)

                } else if ("2".equals(shoppingVideoEntity.video_type)) {
                    //外链,用webview打开
                    val intent = Intent(mContext, WebActivity::class.java)
                    intent.putExtra("title", shoppingVideoEntity.video_title)
                    intent.putExtra("data", shoppingVideoEntity.video_out_link)
                    startActivity(intent)
                }
            }
        })
    }

    override fun loadData(refresh: Boolean) {
        presenter.loadData(refresh, currentPage.toString())
    }

    override fun setData(data: List<ShoppingVideoEntity>?) {
        showContent()
        currentPage++
        contentView.isRefreshing = false
        mAdapter.setNewData(data)
    }

    override fun loadMoreData(data: MutableList<ShoppingVideoEntity>?) {
        currentPage++
        if (data != null && !data.isEmpty()) {
            mAdapter.loadMoreComplete()
            mAdapter.addData(data)
        } else {
            mAdapter.loadMoreEnd()
        }
    }

    override fun onDataFailure(err: String?) {
        mAdapter.loadMoreFail()
    }

}